//
//  AXFMineOrderBtnView.m
//  BEEQUICK
//
//  Created by mac2 on 16/11/19.
//  Copyright © 2016年 SevenGroup. All rights reserved.
//

#import "AXFMineOrderBtnView.h"
#import "AXFFirstCell.h"

@interface AXFMineOrderBtnView ()<AXFFirstCellDelegate>
@property (nonatomic,weak) UIView *yellowLine;

//标记选中按钮
@property(nonatomic,weak)UIButton *seletedBtn;
//记录外部按钮的tag值
@property(assign,nonatomic)NSInteger buttonTag;
@end

@implementation AXFMineOrderBtnView
{
    NSMutableArray <UIButton *>*_btnArray;
}

//创建buttonView的类方法
-(UIView *)btnViewWithButtonTag:(NSInteger )buttonTag{
    self.buttonTag = buttonTag;
    
    [self setupBtnUI];
    
    return self;
}


//按钮点击事件
-(void)clickBtn:(UIButton *)btn{
    //按钮状态的替换
    self.seletedBtn.selected = NO;
    btn.selected = YES;
//    self.seletedBtn = btn;
    
    //小红条的切换
    NSInteger index = [_btnArray indexOfObject:btn];
    [_yellowLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_btnArray[self.buttonTag]).offset((index - self.buttonTag) * _btnArray[0].bounds.size.width);
        
    }];
    [UIView animateWithDuration:.3 animations:^{
        [self layoutIfNeeded];
    }];
    self.seletedBtn = btn;

}
//界面搭建
-(void)setupBtnUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    //创建三个按钮
    //文字
    NSArray *tittleArray = @[@"全部订单",@"待付款",@"待收货",@"待评价"];
    //循环创建
    _btnArray = [NSMutableArray array];
    [tittleArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton cz_textButton:title fontSize:15 normalColor:[UIColor cz_colorWithHex:0x555555] selectedColor:[UIColor redColor]];
        //监听事件
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        if(idx == self.buttonTag){
            btn.selected = YES;
            self.seletedBtn = btn;
        }
        //添加
        [self addSubview:btn];
        [_btnArray addObject:btn];
        
    }];
    //初始化按钮点击状态
    self.seletedBtn = _btnArray[self.buttonTag];
    //小黄条
    UIView *yellowLine = [[UIView alloc]init];
    yellowLine.backgroundColor = [UIColor redColor];
    [self addSubview:yellowLine];
    self.yellowLine = yellowLine;
    
    //布局
    [_btnArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [_btnArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
    }];
    
    [yellowLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.equalTo(_btnArray[self.buttonTag]);
        
        make.width.equalTo(@60);
        
        make.height.equalTo(@2);
    }];
    
}

@end
